package cn.easyutil.veteran.core.logger;

import cn.hutool.core.util.ClassUtil;
import org.apache.ibatis.logging.Log;

/**
 * mybatis的日志打印实现
 */
public class MybatisLogImpl implements Log {

    private final String clazz;

    public MybatisLogImpl(String clazz) {
        this.clazz = clazz;
    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public void error(String s, Throwable e) {
        LoggerUtil.error(parseClass(),e,s);
    }

    @Override
    public void error(String s) {
        LoggerUtil.error(parseClass(),s);
    }

    @Override
    public void debug(String s) {
        LoggerUtil.info(parseClass(),s);
    }

    @Override
    public void trace(String s) {
        //查询语句的具体返回条目会通过trace进行打印
        LoggerUtil.debug(parseClass(),s);
    }

    @Override
    public void warn(String s) {
        LoggerUtil.warn(parseClass(),s);
    }

    private Class<?> parseClass(){
        String clazz = this.clazz.substring(0, this.clazz.lastIndexOf("."));
        try {
            return ClassUtil.loadClass(clazz);
        }catch (Exception e) {
            return this.getClass();
        }
    }
}
